package com.aletto.general_store.ui.adapters;


import android.content.res.Resources;
import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.aletto.general_store.GeneralStoreApplication;
import com.aletto.general_store.R;
import com.aletto.general_store.data.model.Goods;
import com.aletto.general_store.utils.XHelper;
import com.aletto.general_store.utils.image.GlideApp;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.List;

public class GoodsListAdapter extends BaseQuickAdapter<Goods, BaseViewHolder> {

    public GoodsListAdapter() {

        super(R.layout.item_goods_grid);
    }

    public GoodsListAdapter(List<Goods> data) {

        super(R.layout.item_goods_grid, data);
    }

    @Override
    protected void convert(@NonNull BaseViewHolder holder, Goods goods) {
        Resources resources = getContext().getResources();

        holder.setText(R.id.tv_goods_title, goods.getTitle())
                .setText(R.id.tv_goods_price, XHelper.toPrice(goods.getPrice()))
                .setText(R.id.tv_price_unit, goods.getPriceUnit())
                .setText(R.id.tv_sold_number, String.format(resources.getString(R.string.placeholder_goods_sold_number), goods.getSoldNumber()));

        // 加载图片
        List<String> goodsImg = goods.getGoodsImg();
        if (goodsImg != null && !goodsImg.isEmpty()) {
            ImageView ivGoodsCover = holder.getView(R.id.iv_goods_cover);
            GlideApp.with(ivGoodsCover)
                    .load(goodsImg.get(0))
                    .goodsGrid()
                    .into(ivGoodsCover);
        }
    }

}
